<template>
  <div>
    <n-modal
      :show="props.showModal"
      :on-mask-click="maskClick"
      :class="{ 'blow-up': isBlowUp }"
    >
      <n-card
        style="width: 50%; max-height: 60vh; min-width: 650px"
        :title="props.dialogTitle"
        :bordered="false"
        size="huge"
        role="dialog"
        aria-modal="true"
        header-style="padding:20px;padding-bottom:10px;"
        content-style="overflow: hidden; overflow-y: auto;padding:20px;padding-top:0px;"
      >
        <template #header-extra>
          <n-button @click="closeModal" text size="tiny" color="#ff69b4">
            <template #icon>
              <n-icon size="20">
                <Close></Close>
              </n-icon>
            </template>
          </n-button>
        </template>
        <div style="margin: 20px 0px">
          <n-form
            label-placement="left"
            ref="formRef"
            :label-width="80"
            :model="formValue"
            :rules="rules"
            :size="size"
          >
            <n-form-item label="角色名称" path="name">
              <n-input v-model:value="formValue.name" placeholder="请输入名称" />
            </n-form-item>

            <n-form-item label="是否可用">
              <n-switch v-model:value="formValue.enable" size="medium">
                <template #checked> 可用 </template>
                <template #unchecked>禁用 </template>
                <template #checked-icon>
                  <n-icon :component="CheckmarkDoneCircleSharp" />
                </template>
                <template #unchecked-icon>
                  <n-icon :component="CloseCircleOutline" />
                </template>
              </n-switch>
            </n-form-item>
            <n-form-item label="角色描述">
              <n-input
                v-model:value="formValue.description"
                type="textarea"
                placeholder="请输入角色描述"
              />
            </n-form-item>
          </n-form>
        </div>
        <template #action>
          <div style="margin: -15px; display: flex; justify-content: flex-end; gap: 10px">
            <n-button size="small" @click="closeModal">取消</n-button>
            <n-button size="small" type="primary" @click="handleValidateClick">
              确定
            </n-button>
          </div>
        </template>
      </n-card>
    </n-modal>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { Close, CheckmarkDoneCircleSharp, CloseCircleOutline } from "@vicons/ionicons5";
import { useStore } from "vuex";
import { createRoleInfoRequest } from "@/models/permissionsModles.ts";
import { createRole } from "@/api/permissions.ts";

const props = defineProps({
  showModal: {
    type: Boolean,
    default: () => false,
  },
  data: {
    type: Array,
    default: "",
  },
  dialogTitle: {
    type: String,
    default: () => "创建角色",
  },
});
const formRef: any = ref(null);
const isBlowUp = ref(false);
const formValue = ref<createRoleInfoRequest>({
  description: null,
  name: "",
  enable: true,
});
const rules = {
  name: {
    required: true,
    trigger: ["blur", "input"],
    message: "请给角色一个名字",
  },
};

const menuTreeList = ref();

const init = (row: any = null) => {
  menuTreeList.value = [];
  formValue.value.name = "";
  formValue.value.description = null;
  formValue.value.enable = true;
};

const maskClick = () => {
  isBlowUp.value = true;
  setTimeout(() => {
    isBlowUp.value = false;
  }, 500);
};
const emit = defineEmits(["closeModal", "getRoleData"]);

// 假设你有一个方法来切换模态框的显示状态
const closeModal = () => {
  emit("closeModal");
  init();
};
const handleValidateClick = (e) => {
  e.preventDefault();
  formRef.value?.validate((errors) => {
    if (!errors) {
      createRole(formValue.value).then((res) => {
        if (res.code == 200) {
          window.$message.success(res.message);
          closeModal();
          emit("getRoleData");
        } else {
          window.$message.error(res.message);
        }
      });
    } else {
      console.log(formValue.value);
      window.$message.error("表单验证错误");
    }
  });
};
defineExpose({ init });
</script>

<style scoped>
.blow-up {
  animation: scaleAnimation 0.5s infinite;
  transform-origin: center center;
  /* 设置缩放中心为元素中心点 */
}

@keyframes scaleAnimation {
  0% {
    transform: scale(1);
    transform-origin: center center;
  }

  50% {
    transform: scale(1.1);
    /* 在动画中点缩放到1.1倍 */
    transform-origin: center center;
  }

  100% {
    transform: scale(1);
    /* 在动画结束时缩放回原始大小 */
    transform-origin: center center;
  }
}
</style>
